#ifndef QBINARYRPC_SERVER_H
#define QBINARYRPC_SERVER_H

#include "qbinaryrpc_dllprfx.h"
#include "qtcpserver.h"
#include "qvariant.h"
#include "qhostaddress.h"

namespace qbinaryrpc {

class IncomingConnection;

class QBINARYRPC_DLLPRFX Server : public QObject {
  friend class IncomingConnection;    
  Q_OBJECT
public:
  Server(QObject *parent = 0);
  ~Server();  
  QHostAddress getAddress() const;
  quint16 getPort() const;
  bool listen(const QHostAddress &host = QHostAddress::Any, 
              quint16 port = 0);
  bool isListening() const;
  void setAllowedAddresses(const QList<QHostAddress> &allowed_addresses);
  const QList<QHostAddress> &getAllowedAddresses() const;
  QHostAddress getSessionAddress(int session_id) const;
  void registerMethod(const QString &method_name, 
                      QVariant::Type return_type, 
                      const QList<QVariant::Type> &parameters_types);
signals:
  void connect(QHostAddress);
  void disconnect(QHostAddress);
  void createSession(int session_id);
  void destroySession(int session_id);
  void incomingRequest(int session_id,
                       int request_id, 
                       const QString &method_name, 
                       const QVariantList &parameters);
public slots:
  void sendReturnValue(int session_id,
                       int request_id,
                       const QVariant &return_value);
  void sendFault(int session_id,
                 int request_id,
                 int fault_code,
                 const QString &fault_string);
private slots:
  void newConnection();
  void processRequest(const QByteArray &request, QTcpSocket *socket);
  void disconnectSocket(QTcpSocket *socket);
private:
  class Private;
  Private *d;
};

} //namespace qbinaryrpc

#endif //QBINARYRPC_SERVER_H
